//
//  YWRepairCostsCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/8/9.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWRepairCostsCell.h"
#import "Masonry.h"
#import "YWMacro.h"

@interface YWRepairCostsCell()<UITextFieldDelegate>
/*  人工  */
@property (nonatomic , strong) UILabel * costsLabel;
/*  人工费用  */
@property (nonatomic , strong) UITextField * costTextField;
/*  材料  */
@property (nonatomic , strong) UILabel * materialLabel;
/*  材料费用  */
@property (nonatomic , strong) UITextField * materialTextField;
@end

@implementation YWRepairCostsCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpSubviews];
    }
    
    return self;
}

- (void)setUpSubviews{
    UIView *superView = self.contentView;
    [self.costsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(superView.mas_left).offset(10);
        make.top.mas_equalTo(superView.mas_top).offset(10);
        make.height.mas_equalTo(self.costTextField.mas_height);
        make.width.mas_equalTo(@35);
    }];
    
    [self.costTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).offset(10);
        make.left.mas_equalTo(self.costsLabel.mas_right).offset(10);
        make.right.mas_equalTo(superView.mas_centerX).offset(-10);
        make.bottom.mas_equalTo(superView.mas_bottom).offset(-10);
    }];
    
    [self.materialLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).offset(10);
        make.left.mas_equalTo(superView.mas_centerX).offset(10);
        make.height.mas_equalTo(self.costTextField.mas_height);
        make.width.mas_equalTo(@35);
    }];
    
    [self.materialTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).offset(10);
        make.left.mas_equalTo(self.materialLabel.mas_right).offset(10);
        make.right.mas_equalTo(superView.mas_right).offset(-10);
        make.bottom.mas_equalTo(superView.mas_bottom).offset(-10);
    }];
    
}

- (UILabel *)costsLabel{
    if (!_costsLabel) {
        UILabel * label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.text = @"人工:";
        [self.contentView addSubview:label];
        _costsLabel = label;
    }
    return _costsLabel;
}

- (UITextField *)costTextField{
    if (!_costTextField) {
        UITextField * textField = [[UITextField alloc] init];
        textField.font = kContentFont;
        textField.placeholder = @"人工费用";
        textField.keyboardType = UIKeyboardTypeDecimalPad;
        textField.delegate = self;
        textField.tag = 1100;
        [self.contentView addSubview:textField];
        _costTextField = textField;
    }
    return _costTextField;
}

- (UILabel *)materialLabel{
    if (!_materialLabel) {
        UILabel * label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.text = @"材料:";
        [self.contentView addSubview:label];
        _materialLabel = label;
    }
    return _materialLabel;
}

- (UITextField *)materialTextField{
    if (!_materialTextField) {
        UITextField * textField = [[UITextField alloc] init];
        textField.font = kContentFont;
        textField.placeholder = @"材料费用";
        textField.keyboardType = UIKeyboardTypeDecimalPad;
        textField.delegate = self;
        textField.tag = 1101;
        [self.contentView addSubview:textField];
        _materialTextField = textField;
    }
    return _materialTextField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSString *laborCost = nil;
    NSString *materialCost = nil;
    if (textField.tag == 1100) {
        laborCost = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    }else if (textField.tag == 1101){
        materialCost = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    }
    if ([self.delegate respondsToSelector:@selector(passRepairCosts:materialCost:)]) {
        [self.delegate passRepairCosts:laborCost materialCost:materialCost];
    }
}

@end
